package com.share.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.common.util.CommonUtil;
import com.share.common.util.FileUtil;
import com.share.mapper.BizAddressMapper;
import com.share.pojo.bo.BizAddressInfo;
import com.share.pojo.input.AddressQueryInput;
import com.share.pojo.input.AddressSaveInput;
import com.share.pojo.output.JsonResult;
import com.share.pojo.output.PageOutput;
import com.share.service.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * address
 *
 * @author liw
 * @version 1.0
 * @date 2022/6/13 11:10
 */
@Slf4j
@Service
public class BizAddressService extends BaseService {

    @Autowired
    private BizAddressMapper bizAddressMapper;


    /**
     * 查询列表
     *
     * @return data
     */
    public JsonResult<PageOutput<BizAddressInfo>> queryList() {
        PageOutput.Node<BizAddressInfo, BizAddressInfo> node = new PageOutput.Node<BizAddressInfo, BizAddressInfo>()
                .setFun(a -> a);
        LambdaQueryWrapper<BizAddressInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(BizAddressInfo::getDeleteFlag, false);
        node.setList(bizAddressMapper.selectList(qw));
        return JsonResult.success(PageOutput.page(node));
    }


    /**
     * 查询列表
     *
     * @return data
     */
    public JsonResult<PageOutput<BizAddressInfo>> queryListAll(AddressQueryInput vo) {
        PageOutput.Node<BizAddressInfo, BizAddressInfo> node = new PageOutput.Node<BizAddressInfo, BizAddressInfo>()
                .setFun(a -> a);
        LambdaQueryWrapper<BizAddressInfo> qw = new LambdaQueryWrapper<>();
        qw.orderByDesc(BizAddressInfo::getUpdateTime);
        if (!CommonUtil.isEmpty(vo.getName())) {
            qw.like(BizAddressInfo::getName, vo.getName());
        }
        if (!CommonUtil.isEmpty(vo.getPhone())) {
            qw.eq(BizAddressInfo::getPhone, vo.getPhone());
        }
        if (!CommonUtil.isEmpty(vo.getRegisterId())) {
            qw.eq(BizAddressInfo::getRegisterId, vo.getRegisterId());
        }
        if (Boolean.TRUE.equals(vo.getEnable())) {
            Page<BizAddressInfo> objectPage = new Page<>(vo.getPageNo(), vo.getPageSize());
            objectPage.setOptimizeCountSql(false);
            node.setPage(bizAddressMapper.selectPage(objectPage, qw));
        } else {
            node.setList(bizAddressMapper.selectList(qw));
        }
        PageOutput<BizAddressInfo> page = PageOutput.page(node);
        return JsonResult.success(page);
    }

    /**
     * 查询列表
     *
     * @return data
     */
    public void download(AddressQueryInput vo) {
        vo.setEnable(false);
        JsonResult<PageOutput<BizAddressInfo>> result = queryListAll(vo);
        List<BizAddressInfo> bizOrders = result.getData().getList();
        Map<String, Function<BizAddressInfo, String>> map = new LinkedHashMap<>();
        map.put("地址编号", t -> String.valueOf(t.getId()));
        map.put("客户编号", t -> String.valueOf(t.getRegisterId()));
        map.put("名称", BizAddressInfo::getName);
        map.put("详细地址", BizAddressInfo::getDetail);
        map.put("电话", BizAddressInfo::getPhone);
        map.put("省", BizAddressInfo::getCityName1);
        map.put("市", BizAddressInfo::getCityName2);
        map.put("区", BizAddressInfo::getCityName3);
        map.put("创建时间", t -> t.getCreateTime().toString());
        Workbook wb = FileUtil.list2Wb(bizOrders, map);
        FileUtil.outWorkbook(response(), wb);
    }


    /**
     * query address
     *
     * @param id 数据
     * @return 1
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<BizAddressInfo> queryInfo(Long id) {
        return JsonResult.success(bizAddressMapper.selectById(id));
    }

    /**
     * save address
     *
     * @param vo 数据
     * @return 1
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Long> save(AddressSaveInput vo) {
        BizAddressInfo bizAddressInfo = dtoConverter.addressVo2Bo(vo);
        bizAddressMapper.insert(bizAddressInfo);
        return JsonResult.success(bizAddressInfo.getId());
    }

    /**
     * save address
     *
     * @param id 数据
     * @return 1
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult<Long> delete(Long id) {
        LambdaUpdateWrapper<BizAddressInfo> uw = new LambdaUpdateWrapper<>();
        uw.eq(BizAddressInfo::getId, id);
        uw.set(BizAddressInfo::getDeleteFlag, true);
        bizAddressMapper.update(null, uw);
        return JsonResult.success(id);
    }

}
